package br.ufc.cloud;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
import com.google.appengine.api.images.ImagesService;
import com.google.appengine.api.images.ImagesServiceFactory;

public class Serve extends HttpServlet {

	private BlobstoreService blobstoreService = BlobstoreServiceFactory
			.getBlobstoreService();

	public void doGet(HttpServletRequest req, HttpServletResponse res)
			throws IOException {

		res.getWriter().print("<html>");
		res.getWriter().print("<body>");
		
		BlobKey blobKey = new BlobKey(req.getParameter("blob-key"));
		String name = req.getParameter("name");
		String address = req.getParameter("address");
		String phone = req.getParameter("phone");
		String email = req.getParameter("email");

		res.getWriter().print("<h3>Adicionado Com Sucesso!</h3> <br />");
		res.getWriter().print("<table border=\"3\">");
		
		res.getWriter().print("<tr><td>");
		res.getWriter().print("<br />Name: " + name);
		res.getWriter().print("</td></tr>");
		
		res.getWriter().print("<tr><td>");
		res.getWriter().print("<br />Address: " + address);
		res.getWriter().print("</td></tr>");
		
		res.getWriter().print("<tr><td>");
		res.getWriter().print("<br />Phone: " + phone);
		res.getWriter().print("</td></tr>");
		
		res.getWriter().print("<tr><td>");
		res.getWriter().print("<br />Email: " + email);
		res.getWriter().print("</td></tr>");
		
		res.getWriter().print("<tr><td>");
		res.getWriter().print("<br />BlobKey: " + blobKey.getKeyString());
		res.getWriter().print("</td></tr>");
		
		//blobstoreService.serve(blobKey, res);
		
		ImagesService imagesService = ImagesServiceFactory.getImagesService();
		String imageUrl = imagesService.getServingUrl(blobKey);
		
		res.getWriter().print("<tr><td>");
		res.getWriter().println("<br />Photo: " + "<img src=\"" + imageUrl + "\" height=\"100\" width=\"100\" />");
		res.getWriter().print("</td></tr>");
		
		res.getWriter().print("</table>");
		
		res.getWriter().print("<br /> <br /> <input type=\"button\" value=\"Voltar\" onclick=\"history.go(-1)\"");

		res.getWriter().print("</body>");
		res.getWriter().print("</html>");
		

		

	}

}
